﻿using Microsoft.EntityFrameworkCore;
using RbacPermissionManage.DoMain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RbacPermissionManage.Repository
{
    /// <summary>
    /// 上下文类
    /// </summary>
    public class RbacDbContext:DbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="options"></param>
        public RbacDbContext(DbContextOptions<RbacDbContext> options) : base(options)
        {

        }

        /// <summary>
        /// 管理员表
        /// </summary>
        public DbSet<Admin>? Admin { get; set; }
        /// <summary>
        /// 角色表
        /// </summary>
        public DbSet<Role>? Role { get; set; }

        /// <summary>
        /// 用户角色表
        /// </summary>
        public DbSet<AdminRole>? AdminRole { get; set; }

        /// <summary>
        /// 菜单表
        /// </summary>
        public DbSet<Menu>? Menu { get; set; }

        /// <summary>
        /// 角色菜单表
        /// </summary>
        public DbSet<RoleMenu>? RoleMenu { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Admin>(x =>
            {
                x.Property(p => p.UserName).IsRequired().HasMaxLength(50);
                x.Property(p => p.Pwd).IsRequired().HasMaxLength(50);
                x.Property(p => p.Email).HasMaxLength(50);
                x.Property(p => p.FullName).HasMaxLength(50);
                x.Property(p => p.LastLoginIP).HasMaxLength(50);
                x.Property(p => p.Avatar).HasMaxLength(50);
            });

            modelBuilder.Entity<Role>(x =>
            {
                x.Property(x => x.RoleName).IsRequired().HasMaxLength(50);
                x.Property(x => x.RoleCode).IsRequired().HasMaxLength(50);
                x.Property(x => x.Remark).HasMaxLength(500);
            });

            //数据播种SeedData
            modelBuilder.Entity<Role>().HasData(
            new Role
            {
                RoleId = 1,
                RoleName = "超级管理员",
                RoleCode = "superAdmin"
            },
            new Role
            {
                RoleId = 2,
                RoleName = "角色管理员",
                RoleCode = "roleAdmin"
            }
            );

            modelBuilder.Entity<AdminRole>(x =>
            {
                x.HasOne<Admin>().WithMany().HasForeignKey(x => x.AdminId);
                x.HasOne<Role>().WithMany().HasForeignKey(x => x.RoleId);
            });

            modelBuilder.Entity<Menu>(x =>
            {
                x.Property(x => x.MenuName).IsRequired().HasMaxLength(50);
                x.Property(x => x.RouteName).HasMaxLength(50);
                x.Property(x => x.RoutePath).HasMaxLength(50);
                x.Property(x => x.ComponentPath).HasMaxLength(50);
                x.Property(x => x.Icon).HasMaxLength(50);
                x.Property(x => x.Permission).HasMaxLength(50);
                x.Property(x => x.Remark).HasMaxLength(500);
            });

            //数据播种
            modelBuilder.Entity<Menu>().HasData(new Menu
            {
                MenuId = 1,
                MenuName = "系统设置",
                MenuType = MenuType.Directory,
                ParentId = 0,
                RouteName = "system",
                RoutePath = "/system"
            },
            new Menu
            {
                MenuId = 2,
                MenuName = "账号管理",
                MenuType = MenuType.Menu,
                ParentId = 1,
                RouteName = "system:user",
                RoutePath = "user",
                ComponentPath = "system/user/index"
            },
            new Menu
            {
                MenuId = 3,
                MenuName = "查询",
                MenuType = MenuType.Button,
                ParentId = 2,
                Permission = "system:user:page"
            },
            new Menu
            {
                MenuId = 4,
                MenuName = "添加",
                MenuType = MenuType.Button,
                ParentId = 2,
                Permission = "system:user:create"
            },
            new Menu
            {
                MenuId = 5,
                MenuName = "编辑",
                MenuType = MenuType.Button,
                ParentId = 2,
                Permission = "system:user:edit"
            },
            new Menu
            {
                MenuId = 6,
                MenuName = "删除",
                MenuType = MenuType.Button,
                ParentId = 2,
                Permission = "system:user:delete"
            },
            new Menu
            {
                MenuId = 7,
                MenuName = "批量删除",
                MenuType = MenuType.Button,
                ParentId = 2,
                Permission = "system:user:batchdelete"
            }
            );

            modelBuilder.Entity<RoleMenu>(x =>
            {
                x.HasOne<Role>().WithMany().HasForeignKey(x => x.RoleId);
                x.HasOne<Menu>().WithMany().HasForeignKey(x => x.MenuId);
            });

            base.OnModelCreating(modelBuilder);
        }
    }
}
